package com.lp.common.utils;

import cn.hutool.core.util.ObjectUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageInfo;
import com.lp.common.context.UserDetail;
import lombok.extern.slf4j.Slf4j;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

import static com.lp.common.constant.MembersConst.TYPE_MONTH;
import static com.lp.common.constant.MembersConst.TYPE_QUARTER;
import static com.lp.common.constant.OrderConstant.ORDER_TYPE_MEMBERS;

/**
 * @program: backend
 * @description:
 * @author: Ke.Song
 * @since: 2024-03-11 21:35:48
 */
@Slf4j
public class Tools {

    public static <T, E> PageInfo<T> copyPageInfo(PageInfo<T> newObj, Page<E> oldObj) {
        newObj.setTotal(oldObj.getTotal());
        newObj.setPageNum(oldObj.getPageNum());
        newObj.setPageSize(oldObj.getPageSize());
        newObj.setList(new ArrayList<>());
        return newObj;
    }

    public static <T, E> Page<T> copyPage(Page<T> newObj, List<E> oldObj) {
        return copyPage(newObj, (Page<E>)oldObj);
    }

    public static <T, E> Page<T> copyPage(Page<T> newObj, Page<E> oldObj) {
        newObj.setTotal(oldObj.getTotal());
        newObj.setPageNum(oldObj.getPageNum());
        newObj.setPageSize(oldObj.getPageSize());
        return newObj;
    }

    public static BigDecimal stripTrailingZeros(BigDecimal bigDecimal) {
        return bigDecimal == null ? null : new BigDecimal(bigDecimal.stripTrailingZeros().toPlainString());
    }

    public static String sortHandler(String sort) {
        final String split = ",";
        String result = null;
        String asc = "%s asc";
        String desc = "%s desc";
        if (sort != null) {
            for (String s : sort.split(split)) {
                result = result == null ? "" : result + split;
                if (s.startsWith("-")) {
                    result = result + String.format(desc, s.trim().substring(1));
                } else {
                    result = result + String.format(asc, s.trim());
                }
            }
        }
        return result == null ? null : result.trim();
    }

    public static String checkOrderTitle(Integer orderType, Integer membersType, String title) {
        String result;
        if (ObjectUtil.equal(orderType, ORDER_TYPE_MEMBERS)) {
            result = "共读会(%s会员)";
            if (ObjectUtil.equal(membersType, TYPE_MONTH)) {
                result = String.format(result, "月");
            } else if (ObjectUtil.equal(membersType, TYPE_QUARTER)) {
                result = String.format(result, "季");
            } else {
                result = String.format(result, "年");
            }
        } else {
            result = title;
        }
        return result;
    }

    public static String formatCodeScene(String memberCode, Integer id, String categoryCody, Integer classId) {
        StringBuilder builder = new StringBuilder().append("i").append("=").append(memberCode);
        if (id != null) {
            builder.append("&").append("id=").append(id);
        }
        if (classId != null) {
            builder.append("&").append("classId=").append(classId);
        } else if (categoryCody != null) {
            builder.append("&").append("code=").append(categoryCody);
        }
        return builder.toString();
    }
}
